package com.qf.j2205.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @ClassName Druid
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/22 16:51
 * @Version 1.0
 **/
public class DruidUtil  extends Properties {
    static DruidDataSource druidDataSource=null;
    static Connection conn = null;

    static {
        InputStream is = DruidUtil.class.getClassLoader().getResourceAsStream("Druid.properties");
        System.out.println(is);
        Properties properties = new Properties();
        try {
            properties.load(is);
            druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static DruidDataSource getSource(){
        return druidDataSource;
    }

    public static Connection getConnection(){
       if (conn == null) {
           try {
               conn= druidDataSource.getConnection();
           } catch (SQLException e) {
               throw new RuntimeException(e);
           }
       }
        return conn;
    }
}
